package com.guigu.youxuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.youxuan.mapper.BackUserRoleMapper;
import com.guigu.youxuan.pojo.BackUserRole;
import com.guigu.youxuan.service.IBackUserRoleService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@Service
public class BackUserRoleServiceImpl extends ServiceImpl<BackUserRoleMapper, BackUserRole> implements IBackUserRoleService {


    @Override
    public void addUserRole(List<Long> roleIds, Long userId) {
        QueryWrapper<BackUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<BackUserRole> backUserRoleList = this.list(wrapper);
        //删除已有的用户角色
        for (BackUserRole backUserRole : backUserRoleList) {
            this.removeById(backUserRole);
        }
        //重新分配角色
        for(int i=0;i<roleIds.size();i++){
            Long roleId = roleIds.get(i);
            BackUserRole backUserRole = new BackUserRole();
            backUserRole.setRoleId(roleId);
            backUserRole.setUserId(userId);
            this.save(backUserRole);
        }
    }

    @Override
    public List<BackUserRole> queryBackUserRoleByUserId(Long userId) {
        QueryWrapper<BackUserRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<BackUserRole> backUserRoleList = this.list(wrapper);
        return backUserRoleList;
    }
}
